﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using LnSecurityLib;
using System.Data.SqlClient;
using System.Security.Principal;
using Linx.Update;

namespace WindowsFormsApplication5
{
    public partial class Form1 : Form
    {
        public StringBuffer stringbuffer = new StringBuffer();
        public string Conexao { get; set; }
        string strConnectionString = "Diego;FIT_OSCAR;SA;P@ssw0rd;false";
        ConexaoBD conexaodb = new ConexaoBD();

        private string strServer;
        private string strUserDB;
        private string strPasswordDB;
        private string strDatabase;
        private string bWindowsAutentication;


        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            gravaRegistro();
        }


        public void gravaRegistro()
        {
            try
            {

                // cria uma referêcnia para a chave de registro Software

                RegistryKey rk = Registry.LocalMachine.OpenSubKey("Software", true);

                // cria um Subchave como o nome GravaRegistro

                rk = rk.CreateSubKey(@"Linx Sistemas\LinxPOS");

                // grava o caminho na SubChave GravaRegistro
                
                rk.SetValue("ConnectionString", stringbuffer.SaveBuffer(stringbuffer.BuildBuffer(), strConnectionString));

                // fecha a Chave de Restistro registro

                rk.Close();

                MessageBox.Show("Caminho gravado com Sucesso!");



            }

            catch (Exception erro)
            {

                MessageBox.Show("Erro no gravação do Registro: " + erro.Message);

            }

        }

        public void lerRegistro()
        {
            try
            {

                // cria uma referêcnia para a chave de registro Software

                RegistryKey rk = Registry.LocalMachine.OpenSubKey(@"Software\Linx Sistemas\LinxPOS", false);

                // realiza a leitura do registro

                string strPassword;

                strPassword = rk.GetValue("ConnectionString").ToString();
                tbx_caminhoGravado.Text = stringbuffer.LoadBuffer(stringbuffer.BuildBuffer(), strPassword);
                string userName = WindowsIdentity.GetCurrent().Name;
                //Busca o SID do usuario local para ler o Regedit
                NTAccount f = new NTAccount(WindowsIdentity.GetCurrent().Name);
                SecurityIdentifier sid = (SecurityIdentifier)f.Translate(typeof(SecurityIdentifier));
                String sidString = sid.ToString();
                MessageBox.Show(""+f);
                MessageBox.Show("" + sidString);

            }

            catch (Exception erro)
            {

                MessageBox.Show("Erro na leitura do Registro: " + erro.Message);

            }
        } 



        
                private void CreateCommand(string queryString)
                {
                    conexaodb.conectar();

                        SqlCommand command = new SqlCommand(queryString, conexaodb.cn);
                        SqlDataReader reader = command.ExecuteReader();
                        while (reader.Read())
                        {
                            //Console.WriteLine(String.Format("{0}", reader[0]));
                            textBox1.Text = reader[1].ToString();
                        }
                        conexaodb.Desconectar();
                    }



        //cria um dataTable a partir de uma tabela do banco de dados 
                public DataTable LoadTable(string table)
                {
                    string query = "SELECT * FROM [" + table + "]";

                    
                    using (SqlCommand cmd = new SqlCommand(query, conexaodb.cn))
                    {
                        conexaodb.conectar();
                        DataTable dt = new DataTable();
                        dt.Load(cmd.ExecuteReader());
                        conexaodb.Desconectar();
                        return dt;
                    }
                }
                    
                    

        private void button2_Click(object sender, EventArgs e)
        {
            conexaodb.conectar();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            CreateCommand(tbx_caminhoGravado.Text);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            conexaodb.Desconectar();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            string strDomainLogged = Environment.UserDomainName;
            string strUserLogged = Environment.UserName;
            string userName;
            if (strDomainLogged != string.Empty)
            {
                userName = strDomainLogged + "\\" + strUserLogged;
            }
            else 
            {
                userName = strUserLogged;
            }

            NTAccount f = new NTAccount(userName);
            SecurityIdentifier s = (SecurityIdentifier)f.Translate(typeof(SecurityIdentifier));
            String sidString = s.ToString();
           
            MessageBox.Show(userName);
            MessageBox.Show(sidString);

            MessageBox.Show(Environment.UserName);
            
        }

        private void button6_Click(object sender, EventArgs e)
        {
            lerRegistro();
        }

        private void button7_Click(object sender, EventArgs e)
        {
            UpdateNF updateNF = new UpdateNF();
            updateNF.updateNfce(this.strConnectionString, "000000090", "1", "02");

        }

        private void button8_Click(object sender, EventArgs e)
        {
            DataTable datatable = new DataTable();
            datatable = LoadTable("LOJA_NOTA_NFCE");


            foreach (DataRow row in datatable.Rows) // Loop over the rows.
            {
                string CODIGO_FILIAL;
                string NF_NUMERO;
                string SERIE_NF;
                string STATUS_NFE;
                MessageBox.Show("--- Row ---"); // Print separator.
                //MessageBox.Show("Valor: " + row.ItemArray);
                foreach (var item in row.ItemArray) // Loop over the items.
                {
                    //MessageBox.Show("Item: "); // Print label.
                    MessageBox.Show("" + item); // Invokes ToString abstract method.
                    CODIGO_FILIAL = row[0].ToString();
                    NF_NUMERO = row[1].ToString();
                    SERIE_NF = row[2].ToString();
                    STATUS_NFE = row[3].ToString();
                   
                   
                }


               
                 
            }
            Console.Read(); // Pause.
        }




    }
}
